package com.googlecode.mgwt.examples.showcase.client.activities;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
import com.googlecode.mgwt.examples.showcase.client.AppController;
import com.googlecode.mgwt.examples.showcase.client.ClientFactory;
import com.googlecode.mgwt.examples.showcase.client.DetailActivity;
import com.googlecode.mgwt.examples.showcase.client.ServiceCallRenderer;
import com.googlecode.mgwt.examples.showcase.client.activities.animationdone.ReportDetailesPlace;
import com.googlecode.mgwt.examples.showcase.client.activities.elements.ReportDetailsView;
import com.googlecode.mgwt.examples.showcase.client.event.ActionEvent;
import com.googlecode.mgwt.examples.showcase.client.event.ActionNames;
import com.googlecode.mgwt.examples.showcase.client.rpc.AppAsyncCallback;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog.ConfirmCallback;

public class ReportDetailsActivity extends DetailActivity implements Activity {

	
	private ClientFactory clientFactory;
	private ReportDetailesPlace place;
	public ReportDetailsActivity(ClientFactory clientFactory,ReportDetailesPlace place) {
		super(clientFactory.getReportDetailsView(), "ReportDetailsActivity");
		this.clientFactory = clientFactory;
		this.place = place;
	}

	@Override
	public String mayStop() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCancel() {
		// TODO Auto-generated method stub

	}

	
	@Override
	public void start(AcceptsOneWidget panel, final EventBus eventBus) {
		super.start(panel, eventBus);
	final ReportDetailsView view = (ReportDetailsView)detailView;

	view.getBackbuttonText().setText(clientFactory.getUserProfile().getCurrentUser().getLabels().back());
	view.getHeader().setText(clientFactory.getUserProfile().getCurrentUser().getLabels().requestDatilsTitle());
	view.getMainButtonText().setText("Nav");
	view.setReportDetailes(place.getServiceCallDTO());
	panel.setWidget(view);
	

	
	addHandlerRegistration(view.getCloseCallButton().addTapHandler(new TapHandler() {

		@Override
		public void onTap(TapEvent event) {
			ServiceCallRenderer renderer = AppController.getClientFactory().getUserProfile().getCurrentUser().getServiceCallRenderer();
			view.confirm(renderer.getDisplayName(place.getServiceCallDTO()), 
					clientFactory.getLabelResource().closeCallConfirmation(), new ConfirmCallback() {

				@Override
				public void onOk() {
					view.setBussy(true);
					clientFactory.getService().remoceCall(place.getServiceCallDTO().getKey(), new AppAsyncCallback<Void>() {

						@Override
						public void onInnerSuccess(Void aResult) {
							view.setBussy(false);
							ActionEvent.fire(eventBus, ActionNames.BACK);
						}
						@Override
						public void onFailure(Throwable caught) {
							view.setBussy(false);
						}
					});
				}

				@Override
				public void onCancel() {

				}
			});

		}
	}));
	}


}
